package com.asha.md360player4android.vr;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fuyao on 2017/3/21.
 */

public class SharePreferenceManager {
    private static final String TAG = "SharePreferenceManager";
    private static boolean DEBUG_LOG = Log.isLoggable("SharePreferenceManager", Log.DEBUG);
    private static final String SETTING_CALIBRATE_ORIGIN_W = "calibrate_origin_w";
    private static final String SETTING_CALIBRATE_ORIGIN_H = "calibrate_origin_h";
    private static final String SETTING_CALIBRATE_CENTER_X = "calibrate_x";
    private static final String SETTING_CALIBRATE_CENTER_Y = "calibrate_y";
    private static final String SETTING_CALIBRATE_RADIUS = "calibrate_r";

    private static SharedPreferences mSharedPreferences;

    private static SharedPreferences getSharedPreferences(Context context) {
        if (null == mSharedPreferences) {
            mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        }
        return mSharedPreferences;
    }

    public static float[] getCalibrate(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        float[] calibrate = new float[5];
        calibrate[0] = sp.getFloat(SETTING_CALIBRATE_ORIGIN_W, -1f);
        if (-1f == calibrate[0]) {
            return null;
        }
        calibrate[1] = sp.getFloat(SETTING_CALIBRATE_ORIGIN_H, -1f);
        calibrate[2] = sp.getFloat(SETTING_CALIBRATE_RADIUS, -1f);
        calibrate[3] = sp.getFloat(SETTING_CALIBRATE_CENTER_X, -1f);
        calibrate[4] = sp.getFloat(SETTING_CALIBRATE_CENTER_Y, -1f);
        return calibrate;
    }

    public static void setCalibrate(Context context, float[] calibrate) {
        if (null != calibrate && calibrate.length == 5) {
            SharedPreferences.Editor editor = getSharedPreferences(context).edit();
            editor.putFloat(SETTING_CALIBRATE_ORIGIN_W, calibrate[0]);
            editor.putFloat(SETTING_CALIBRATE_ORIGIN_H, calibrate[1]);
            editor.putFloat(SETTING_CALIBRATE_RADIUS, calibrate[2]);
            editor.putFloat(SETTING_CALIBRATE_CENTER_X, calibrate[3]);
            editor.putFloat(SETTING_CALIBRATE_CENTER_Y, calibrate[4]);
            editor.apply();
        }
    }

    public static List<String> getSDCardIds(Context context) {
        SharedPreferences sp = context.getSharedPreferences("sdcard_ids", Context.MODE_PRIVATE);
        List<String> idList = new ArrayList<>();
        int size = sp.getInt("size", 0);
        for (int i = 0; i < size; i++) {
            idList.add(sp.getString("index_" + (i + 1), ""));
        }
        return idList;
    }

    public static void addSDCardId(Context context, String id) {
        SharedPreferences sp = context.getSharedPreferences("sdcard_ids", Context.MODE_PRIVATE);
        int size = sp.getInt("size", 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("index_" + (size + 1), id);
        editor.putInt("size", size + 1);
        editor.apply();
    }

}
